package com.example.tbplus14.Test;

import lombok.Data;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

public class TestPaChong {

    public static final String JSON = "";

    public static void main(String[] args)throws Exception {
//        System.out.println(pachong("http://www.77shuku.org/chapter/166810/65223164.html"));
//        test1();
//        test2();
        test3();
    }

    public static String pachong(String urlString)throws Exception{
        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");

        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) { // success
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuilder response = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line).append('\n');
            }
            reader.close();
            return response.toString();
        } else {
            throw new RuntimeException("Failed : HTTP error code : " + responseCode);
        }
    }

    public static void test1(){
        String url = "http://www.77shuku.org/chapter/166810/%s.html";

        for (int i = 65223164; i < 99999999; i++) {
            try {
                Document doc = Jsoup.connect(String.format(url, i))
                        .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
                        .timeout(5000)
                        .get();

                // 提取小说标题
                String title = doc.getElementById("chapter_title_924915").text();
                System.out.println("标题: " + title);

                // 提取小说正文
                Element contentElement = doc.getElementById("ChapterContents");
                if (contentElement != null) {
                    // 处理正文内容，移除不必要的元素
                    Elements ads = contentElement.select("div, script, style, a");
                    ads.remove();

                    String content = contentElement.text();
                    System.out.println("正文: " + content);
                } else {
                    System.out.println("未找到正文内容");
                }
            } catch (IOException e) {
                System.out.println("爬取网页时出错: " + e.getMessage());
                e.printStackTrace();
            }
        }

    }

    public static void test2() throws IOException {
        String url = "http://www.77shuku.org/chapter/166810/%s.html";

        String txtname = "C:\\Users\\tb14plus\\Desktop\\jar\\武碎星河.txt";
        PrintStream ps = new PrintStream(Files.newOutputStream(new File(txtname).toPath()));

        for (int i = 65228363; i < 99999999; i++) {
            try {
                Document doc = Jsoup.connect(String.format(url, i))
                        .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
                        .timeout(5000)
                        .get();

                // 提取小说标题
                String title = doc.getElementById("chapter_title_924915").text();
                System.out.println("标题: " + title);
                ps.append(title);
                ps.append("\n");

                // 提取小说正文
                Element contentElement = doc.getElementById("ChapterContents");
                if (contentElement != null) {
                    // 处理正文内容，移除不必要的元素
                    Elements ads = contentElement.select("div, script, style, a");
                    ads.remove();

                    String content = contentElement.text();
                    System.out.println("正文: " + content);
                    ps.append(content);
                    ps.append("\n");
                } else {
                    System.out.println("未找到正文内容");
                }
            } catch (IOException e) {
                System.out.println("爬取网页时出错: " + e.getMessage());
                e.printStackTrace();
            }
        }
        ps.close();

    }

    public static void test3() throws Exception {
        String url = "http://www.77shuku.org/chapter/166810/%s.html";

        List<CompletableFuture<TxtContent>> list = new ArrayList<>();

        CompletableFutureThreadPool completableFutureThreadPool = new CompletableFutureThreadPool();
        ThreadPoolExecutor threadPoolExecutor = completableFutureThreadPool.poolExecutor();

        for (int i = 65223164; i < 65226596; i++) {
            int finalI = i;
            CompletableFuture<TxtContent> future = CompletableFuture.supplyAsync(
                    () -> getText(String.format(url, finalI), finalI),
                    threadPoolExecutor);
            list.add(future);
        }

        CompletableFuture.allOf(list.toArray(new CompletableFuture[0])).join();
        List<TxtContent> txts = new ArrayList<>();
        for (CompletableFuture<TxtContent> txtContentCompletableFuture : list) {
            TxtContent txtContent = txtContentCompletableFuture.get();
            if (null != txtContent){
                txts.add(txtContent);
            }
        }

        String txtname = "C:\\Users\\tb14plus\\Desktop\\jar\\武碎星河2.txt";
        PrintStream ps = new PrintStream(Files.newOutputStream(new File(txtname).toPath()));
        txts.sort(Comparator.comparing(TxtContent::getSort));
        for (TxtContent txt : txts) {
            ps.append(txt.getTitle());
            ps.append("\n");
            ps.append(txt.getContent());
            ps.append("\n");
        }
        ps.close();

    }


    private static TxtContent getText(String url, int i){
        try {
            Thread.sleep(50);
            Document doc = Jsoup.connect(String.format(url, i))
                    .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
                    .timeout(5000)
                    .get();

            // 提取小说标题
            String title = doc.getElementById("chapter_title_924915").text();
            System.out.println("标题: " + title);

            // 提取小说正文
            Element contentElement = doc.getElementById("ChapterContents");
            if (contentElement != null) {
                // 处理正文内容，移除不必要的元素
                Elements ads = contentElement.select("div, script, style, a");
                ads.remove();
                String content = contentElement.text();
                System.out.println("正文: " + content);

                String sort = title.substring(title.indexOf("第") + 1, title.indexOf("章"));
                return new TxtContent(title, content, Integer.parseInt(sort));
            } else {
                System.out.println("未找到正文内容");
            }
        } catch (Exception e) {
            System.out.println("爬取网页时出错: " + e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Data
    private static class TxtContent{
        private String title;
        private String content;
        private int sort;

        public TxtContent(String title, String content, int sort) {
            this.title = title;
            this.content = content;
            this.sort = sort;
        }
    }
}
